package com.paipaideli.common.utils;


import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.util.TypedValue;

import com.paipaideli.MyApplication;

import java.security.MessageDigest;

import static android.content.Context.TELEPHONY_SERVICE;


/**
 * Created by on 2015/10/12 0012.
 * 检测网络连接状态
 */
public class ConnecStatusUtils {
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dpToPx(Resources res, float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                res.getDisplayMetrics());
    }

    public static boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) MyApplication.getInstanse().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo current = cm.getActiveNetworkInfo();
        if (current == null) {
            return false;
        }
        return current.isAvailable();
    }

    /**
     * 获取手机唯一识别号 多种号拼接加密
     */
    public static String getAndroidId(final Context context) {
        String m_szUniqueID = new String();
        String szImei = "";
        try {
            try{
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    final TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                    if (manager.getDeviceId() == null || manager.getDeviceId().equals("")) {
                        if (Build.VERSION.SDK_INT >= 23) {
                            szImei = manager.getDeviceId(0);
                        }
                    } else {
                        szImei = manager.getDeviceId();
                    }
                }
            }catch (Exception e){
                e.getMessage();
            }
            String m_szAndroidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
            m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            String m_szBTMAC = "";
            if (m_BluetoothAdapter != null) {
                m_szBTMAC = m_BluetoothAdapter.getAddress();
            }
            String m_szLongID = szImei + m_szAndroidID + m_szBTMAC;
            // compute md5
            MessageDigest m = MessageDigest.getInstance("MD5");

            m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
            // get md5 bytes
            byte p_md5Data[] = m.digest();
            for (int i = 0; i < p_md5Data.length; i++) {
                int b = (0xFF & p_md5Data[i]);
                // if it is a single digit, make sure it have 0 in front (proper padding)
                if (b <= 0xF)
                    m_szUniqueID += "0";
                // add number to string
                m_szUniqueID += Integer.toHexString(b);
            }   // hex string to uppercase
            m_szUniqueID = m_szUniqueID.toUpperCase();
        } catch (Exception e) {
            e.getMessage();
        }
        return m_szUniqueID;
    }
}
